#@admin_layout()
#define admin_body()
<div class="layui-btn-group user-list">
    <button class="layui-btn" data-type="getCheckData">获取选中行数据</button>
    <button class="layui-btn" data-type="getCheckLength">获取选中数目</button>
    <button class="layui-btn" data-type="isAll">验证是否全选</button>
</div>

<table id="user-list" lay-filter="test"></table>

<script type="text/html" id="barOpt">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
#end

#include("userEdit.html")
#@admin_user_edit()

#define admin_script()
<script>
    layui.use(['table', 'layer', 'element', 'form'], function () {
        var table = layui.table,
            layer = layui.layer,
            element = layui.element,
            form = layui.form;

        table.render({
            elem: '#user-list',
            url: '/user/list',
            cols: [[
                {type:'checkbox'},
                {field: 'id', title: 'ID', sort: true},
                {field: 'name', title: '姓名'},
                {field: 'nickname', title: '昵称'},
                {field: 'sex', title: '性别'},
                {field: 'tel', title: '电话'},
                {title: '操作', fixed: 'right', width:178, align:'center', toolbar: '#barOpt'}
            ]],
            cellMinWidth: 80,
            page: true
        });

        table.on('checkbox(test)', function (obj) {
            console.log(obj);
        });

        table.on('tool(test)', function (obj) {
            var data = obj.data;

            if (obj.event === 'detail') {
                layer.msg("ID:" + data.id + "的查看操作！");
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 1,
                    title: '用户编辑',
                    area:["50%"],
                    content: $('#user-edit'),
                    success: function (layero, index) {
                        form.val('form-user-edit', {
                            "user.id": data.id,
                            "user.name": data.name,
                            "user.nickname": data.nickname,
                            "user.sex": data.sex.toString(),// Number需要转成字符串
                            "user.tel": data.tel,
                            "user.email": data.email,
                            "user.roleId": data.roleid,
                            "user.bankId": data.bankid
                        });
                    }
                });
            } else if (obj.event === 'del') {
                layer.confirm('确定删除？', function (index) {
                   obj.del();
                    layer.close(index);
                });
            }
        });

        var $ = layui.$, active = {
            getCheckData: function(){ //获取选中数据
                var checkStatus = table.checkStatus('id')
                    ,data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
            ,getCheckLength: function(){ //获取选中数目
                var checkStatus = table.checkStatus('id')
                    ,data = checkStatus.data;
                layer.msg('选中了：'+ data.length + ' 个');
            }
            ,isAll: function(){ //验证是否全选
                var checkStatus = table.checkStatus('id');
                layer.msg(checkStatus.isAll ? '全选': '未全选')
            }
        };

        $('.user-list .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听提交
        form.on('submit(from-user-submit)', function(data){
            /*$.post({
                url: '/user/save',
                data: data,
                success: function (rep) {
                    layer.msg(JSON.stringify(data));
                }
            });*/
            /*layer.msg(JSON.stringify(data));*/
            //table.render();
            return false;
        });

    });
</script>
#end